#----------------------------------*-sh-*--------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     | Website:  https://openfoam.org
#   \\  /    A nd           | Copyright (C) 2011-2024 OpenFOAM Foundation
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM.
#
#     OpenFOAM is free software: you can redistribute it and/or modify it
#     under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.
#
#     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
#     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#     for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
#
# File
#     config.csh/paraview
#
# Description
#     Setup file for paraview-[4-5].x
#     Sourced from OpenFOAM-<VERSION>/etc/cshrc or from foamPV alias
#
#------------------------------------------------------------------------------

# Clean the PATH
set cleaned=`$WM_PROJECT_DIR/bin/foamCleanPath "$PATH" \
            "/opt/paraviewopenfoam \
            $WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/cmake- \
            $WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/ParaView-" \
            `
if ( $status == 0 ) setenv PATH $cleaned

# Detect the most recent version of cmake available and add to the PATH
set cmakeDir=`eval _foamMostRecentDir "$WM_THIRD_PARTY_DIR"/platforms/$WM_ARCH$WM_COMPILER/cmake-*`
if ( "$cmakeDir" != "" ) then
    setenv PATH $cmakeDir/bin:$PATH
endif
unset cmakeDir

# Set up the paraview environment
switch ("$ParaView_TYPE")
case none:

    unset PV_PLUGIN_PATH
    breaksw

case system:

    # Look for a paraview installation
    set pvserverExe=`which pvserver 2> /dev/null`
    if ( $status == 0 ) then
        set pvserverBinDir=`dirname $pvserverExe`
        set paraviewBinDir=`cd $pvserverBinDir && pwd -P`
    endif

    # Set the environment
    if ( $?paraviewBinDir ) then
    if ( -d "$paraviewBinDir" ) then
        setenv ParaView_DIR `dirname paraviewBinDir`
        export ParaView_LIB_DIR `unset LD_LIBRARY_PATH && \
                                 ldd $paraviewBinDir/paraview | \
                                 grep -o "/.*/libpqCore-pv.*.so" | \
                                 xargs dirname`
        setenv ParaView_VERSION `unset LD_LIBRARY_PATH && \
                                 pvserver --version 2> /dev/null | \
                                 awk '{print $NF}'`
        setenv ParaView_MAJOR `echo $ParaView_VERSION | sed 's/\.[^\.]\+$//g'`
        setenv ParaView_INCLUDE_DIR $ParaView_DIR/include/paraview-$ParaView_MAJOR
        setenv PV_PLUGIN_PATH $FOAM_LIBBIN/paraview-$ParaView_MAJOR

        # Add to the library path
        setenv LD_LIBRARY_PATH ${ParaView_LIB_DIR}:${PV_PLUGIN_PATH}:${LD_LIBRARY_PATH}
    endif
    endif

    unset pvserverExe paraviewBinDir
    breaksw

case paraviewopenfoam:

    # Look for a paraview installation. The version should be set externally.
    if ( $?ParaView_VERSION ) then
        setenv ParaView_MAJOR `echo $ParaView_VERSION | sed 's/\.[^\.]\+$//g'`
        set paraviewDir=/opt/paraviewopenfoam`echo $ParaView_MAJOR | tr -d '.'`
    endif

    # Set the environment
    if ( $?paraviewDir ) then
    if ( -d "$paraviewDir" ) then
        setenv ParaView_DIR $paraviewDir
        setenv ParaView_LIB_DIR `echo "$ParaView_DIR"/lib* | tr ' ' ':'`
        setenv ParaView_INCLUDE_DIR $ParaView_DIR/include/paraview-$ParaView_MAJOR
        setenv PV_PLUGIN_PATH $FOAM_LIBBIN/paraview-$ParaView_MAJOR

        # Add to the path and the library path
        setenv PATH $ParaView_DIR/bin:${PATH}
        setenv LD_LIBRARY_PATH ${ParaView_LIB_DIR}:${PV_PLUGIN_PATH}:${LD_LIBRARY_PATH}
    endif
    endif

    unset paraviewDir
    breaksw

case OpenFOAM:
case ThirdParty:

    # Look for a paraview installation
    if ( ! $?ParaView_VERSION ) then
        set paraviewDir=`eval _foamMostRecentDir "$WM_THIRD_PARTY_DIR"/platforms/$WM_ARCH$WM_COMPILER/ParaView-*`
    else
        set paraviewDir=$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER/ParaView-$ParaView_VERSION
    endif

    # Set the environment
    if ( -d "$paraviewDir" ) then
        setenv ParaView_DIR $paraviewDir
        setenv ParaView_LIB_DIR `echo "$ParaView_DIR"/lib* | tr ' ' ':'`
        setenv ParaView_VERSION `echo $ParaView_DIR | sed 's/.*ParaView-//g'`
        setenv ParaView_MAJOR `echo $ParaView_VERSION | sed 's/\.[^\.]\+$//g'`
        setenv ParaView_INCLUDE_DIR $ParaView_DIR/include/paraview-$ParaView_MAJOR
        setenv PV_PLUGIN_PATH $FOAM_LIBBIN/paraview-$ParaView_MAJOR

        # Add to the path and the library path
        setenv PATH $ParaView_DIR/bin:${PATH}
        setenv LD_LIBRARY_PATH ${ParaView_LIB_DIR}:${PV_PLUGIN_PATH}:${LD_LIBRARY_PATH}

        # Add in python libraries if required
        set paraviewPython=$ParaView_DIR/Utilities/VTKPythonWrapping
        if ( -r $paraviewPython ) then
            if ( $?PYTHONPATH ) then
                setenv PYTHONPATH $PYTHONPATH:$paraviewPython:$ParaView_LIB_DIR
            else
                setenv PYTHONPATH $paraviewPython:$ParaView_LIB_DIR
            endif
        endif
        unset paraviewPython
    endif

    unset paraviewDir
    breaksw

endsw

# Report
if ( $?ParaView_DIR ) then
if ( -d "$ParaView_DIR" && $?FOAM_VERBOSE && $?prompt ) then
    echo "Using paraview"
    echo "    ParaView_DIR         : $ParaView_DIR"
    echo "    ParaView_LIB_DIR     : $ParaView_LIB_DIR"
    echo "    ParaView_INCLUDE_DIR : $ParaView_INCLUDE_DIR"
    echo "    PV_PLUGIN_PATH       : $PV_PLUGIN_PATH"
endif
endif

unset cleaned

#------------------------------------------------------------------------------
